import 'package:enjoy_plus/models/house_list_model.dart';
import 'package:enjoy_plus/pages/house/components/house_item.dart';
import 'package:enjoy_plus/utils/eventbus.dart';
import 'package:enjoy_plus/utils/http.dart';
import 'package:enjoy_plus/utils/logger.dart';
import 'package:flutter/material.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  List<DatumItem> rooms = [];
  bool isLoading = true;
  // GET /room
  getRoomList() async {
    setState(() {
      isLoading = true;
    });
    final res = await http.get<List<DatumItem>>('/room',
        fromJson: (json) =>
            List<DatumItem>.from(json.map((x) => DatumItem.fromJson(x))));
    logger.d(res.data.toString());
    rooms = res.data;
    setState(() {
      isLoading = false;
    });
  }

  @override
  void initState() {
    super.initState();
    getRoomList();
    eventBus.on<RefreshRoomList>().listen((event) {
      getRoomList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromARGB(255, 241, 238, 238),
        appBar: AppBar(
          title: const Text('我的房屋'),
          backgroundColor: const Color(0xFFF1F2F6),
          centerTitle: true,
          leading: IconButton(
              onPressed: () => Navigator.pop(context),
              icon: const Icon(Icons.arrow_back_ios)),
        ),
        body: Container(
            color: const Color(0xFFF1F2F6),
            child: Stack(children: [
              Padding(
                  padding:
                      const EdgeInsets.only(bottom: 80, left: 10, right: 10),
                  child: RefreshIndicator(
                    onRefresh: () async {
                      await getRoomList();
                    },
                    child: AnimatedSwitcher(
                      duration: const Duration(milliseconds: 350),
                      child: isLoading
                          ? const Center(
                              child:
                                  CircularProgressIndicator(color: Colors.blue))
                          : ListView.separated(
                              physics: const AlwaysScrollableScrollPhysics(),
                              itemBuilder: (BuildContext context, int index) {
                                return InkWell(
                                    onTap: () => Navigator.pushNamed(
                                        context, '/houseDetail',
                                        arguments: rooms[index].id),
                                    child: HouseItemCard(room: rooms[index]));
                              },
                              separatorBuilder:
                                  (BuildContext context, int index) {
                                return Container(
                                  height: 10,
                                  color: Colors.transparent,
                                );
                              },
                              itemCount: rooms.length),
                    ),
                  )),
              Positioned(
                  bottom: 0,
                  left: 0,
                  right: 0,
                  child: Container(
                      padding: const EdgeInsets.all(10),
                      color: Colors.transparent,
                      child: Row(children: [
                        Expanded(
                            child: ElevatedButton(
                                style: ElevatedButton.styleFrom(
                                    backgroundColor: Colors.white,
                                    foregroundColor: Colors.blue),
                                onPressed: () => Navigator.pushNamed(
                                    context, '/houseGetPostion'),
                                child: const Column(children: [
                                  SizedBox(height: 5),
                                  Icon(Icons.add),
                                  Text('添加房屋'),
                                  SizedBox(height: 5),
                                ])))
                      ])))
            ])));
  }

  @override
  void dispose() {
    // 取消订阅
    eventBus.destroy();
    super.dispose();
  }
}
